package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.FixReportDao;
import com.student.db.FixReportEntity;
import com.student.db.PushEntity;
import com.student.dms.databinding.ActivityEditPushBinding;
import com.student.dms.databinding.ActivityFixHistoryBinding;
import com.student.dms.databinding.ItemFixHistoryBinding;
import com.student.dms.databinding.ItemPushBinding;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ActivityFixHistory extends BaseActivity {

    private ActivityFixHistoryBinding activityFixHistoryBinding;
    private BaseQuickAdapter<FixReportEntity, VH> baseQuickAdapter;
    private AppDatabase roomDatabase;
    private FixReportDao fixReportDao;
    private AccountDao accountDao;
    private AccountEntity accountEntity;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityFixHistoryBinding = ActivityFixHistoryBinding.inflate(getLayoutInflater());
        setContentView(activityFixHistoryBinding.getRoot());
        SharedPreferences sharedPreferences =getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        accountDao= roomDatabase.provideAccountDao();
        Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
        disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
            this.accountEntity=accountEntity;
        }, studentFlowThrowable -> {
            ToastUtils.showToast(ActivityFixHistory.this, "用户不存在，请注册！");
        }));
        activityFixHistoryBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        fixReportDao = roomDatabase.provideFixReportDao();
        baseQuickAdapter = new BaseQuickAdapter<FixReportEntity, VH>() {
            @NonNull
            @Override
            protected VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                VH vh = new VH(ItemFixHistoryBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull VH vh, int position, @Nullable FixReportEntity itemData) {
                vh.itemViewBidnng.name.setText("姓名：" + itemData.getUsername());
                vh.itemViewBidnng.roomNo.setText("寝室号：" + itemData.getRoomNo());
                vh.itemViewBidnng.fixName.setText("报修物品：" + itemData.getFixName());
                vh.itemViewBidnng.fixReason.setText("报修原因：" + itemData.getFixReason());
                String submitTime = itemData.getSubmitTime();
                if (!TextUtils.isEmpty(submitTime)) {
                    vh.itemViewBidnng.submitTime.setText("提交时间：" + itemData.getSubmitTime());
                } else {
                    vh.itemViewBidnng.submitTime.setText("提交时间：");
                }
                String fixTime = itemData.getFixTime();
                if (!TextUtils.isEmpty(fixTime)) {
                    vh.itemViewBidnng.fixTime.setText("维修时间：" + itemData.getFixTime());
                } else {
                    vh.itemViewBidnng.fixTime.setText("维修时间：");
                }
                int fixStatus = itemData.getFixStatus();
                String fixStatu;
                if (fixStatus == 0) {
                    fixStatu = "状态：未修";
                } else {
                    fixStatu = "状态：已修";
                    vh.itemViewBidnng.submit.setVisibility(View.GONE);
                }
                vh.itemViewBidnng.status.setText(fixStatu);

                if(accountEntity!=null && accountEntity.getType()==1){
                    vh.itemViewBidnng.submit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            String time = simpleDateFormat.format(new Date());
                            itemData.setFixTime(time);
                            itemData.setFixStatus(1);
                            //点击报修
                            disposable.add(fixReportDao.insert(itemData).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                                        ToastUtils.showToast(ActivityFixHistory.this, "维修成功！");
                                        baseQuickAdapter.notifyDataSetChanged();
                                    }
                            ));
                        }
                    });
                }

            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        activityFixHistoryBinding.recyclerView.setAdapter(build.getAdapter());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityFixHistory.this);
        activityFixHistoryBinding.recyclerView.setLayoutManager(linearLayoutManager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(ActivityFixHistory.this, DividerItemDecoration.VERTICAL);
        activityFixHistoryBinding.recyclerView.addItemDecoration(dividerItemDecoration);
        disposable.add(fixReportDao.getFixReports().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fixReportEntities -> {
                    baseQuickAdapter.submitList(fixReportEntities);
                }, error -> {

                }
        ));
    }


    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemFixHistoryBinding itemViewBidnng;

        public VH(@NonNull ItemFixHistoryBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }
}
